home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / nild.exe / NUMINP.CPP < prev    next >
C/C++ Source or Header  |  1992-08-29  |  4KB  |  161 lines

  1. // Copyright (C) 1992 James H. Price, All rights reserved
  2. //
  3. //  NUMINP.CPP
  4. //
  5. //    Member functions for NumInputLine class
  6. //
  7.  
  8. #define Uses_TEvent
  9. #define Uses_TInputLine
  10. #define Uses_TKeys
  11. #include <tv.h>
  12.  
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16.  
  17. #include "numinp.h"
  18.  
  19. template <class Type>
  20. void NumInputLine<Type>::NumInputLine( const TRect& r, int maxLen,
  21.   char aMaskChar ) : TInputLine( r, maxLen ), maskChar( aMaskChar )
  22. {
  23. }
  24.  
  25. template <class Type>
  26. void NumInputLine<Type>::handleEvent( TEvent& event )
  27. {
  28.   if( event.what == evKeyDown )
  29.   {
  30.     ushort keyCode = event.keyDown.keyCode;
  31.     switch( keyCode )
  32.     {
  33.       case kbShiftTab:      // save these for TInputLine
  34.       case kbTab:
  35.       case kbBack:
  36.       case kbEnter:
  37.       case kbEsc:
  38.         break;
  39.       default:
  40.         if( event.keyDown.charScan.charCode )  // don't handle extended keys
  41.         {
  42.           if( !isValidChar( event ) )     // check valid characters
  43.             clearEvent( event );          // if not valid, ignore
  44.         }
  45.     }
  46.   }
  47.   TInputLine::handleEvent( event );
  48. }
  49.  
  50. template <class Type>
  51. Boolean NumInputLine<Type>::isValidChar( TEvent& event )
  52. {
  53.   uchar key = event.keyDown.charScan.charCode;
  54.  
  55.   switch( maskChar )
  56.   {
  57.     case 'u':                   // unsigned int or long
  58.       if( !isdigit( key ) )
  59.         return False;
  60.       break;
  61.     case 'd':                   // signed int or long
  62.       if( key == '-' )
  63.       {
  64.         if( curPos && curPos == selEnd )  // accept '-' for initial keystroke
  65.           return True;
  66.         if ( curPos > 0 )                 // don't accept past first position
  67.           return False;
  68.         if( *data == '-' )                // don't take more than one '-'
  69.           return False;
  70.       }
  71.       else if( !isdigit( key ) )
  72.         return False;
  73.       break;
  74.     case 'f':                     // signed double or float
  75.       if( key == '-' )
  76.       {
  77.         if( curPos && curPos == selEnd )  // accept '-' for initial keystroke
  78.           return True;
  79.         if ( curPos > 0 )                 // don't accept past first position
  80.           return False;
  81.         if( *data == '-' )                // don't take more than one '-'
  82.           return False;
  83.       }
  84.       else if( key == '.' )
  85.       {
  86.         if( curPos && curPos == selEnd )  // accept '.' for initial keystroke
  87.           return True;
  88.         if( strchr( data, '.' )  )        // don't take more than one '.'
  89.           return False;
  90.       }
  91.       else if( !isdigit( key ) )
  92.         return False;
  93.       break;
  94.     default:
  95.       break;
  96.   }
  97.   return True;
  98. }
  99.  
  100. template <class Type>
  101. void NumInputLine<Type>::setData( void *rec )
  102. {
  103.   numToStr( (Type *)rec, data );
  104. }
  105.  
  106. template <class Type>
  107. void NumInputLine<Type>::getData( void *rec )
  108. {
  109.   Type val = strToNum( data );
  110.   memmove( rec, &val, dataSize() );
  111. }
  112.  
  113. ////////////////////////////////////////////////////////////////////////
  114. /////////////////////////// IntInputLine ///////////////////////////////
  115. ////////////////////////////////////////////////////////////////////////
  116.  
  117. IntInputLine::IntInputLine( const TRect& r, int maxLen, char aMaskChar )
  118.   : NumInputLine<int>( r, maxLen, aMaskChar )
  119. {
  120. }
  121.  
  122. void IntInputLine::numToStr( int *rec, char *buf )
  123. {
  124.   itoa( *rec, buf, 10 );    // no bounds or overflow checking!!
  125. }
  126.  
  127. int IntInputLine::strToNum( char *s )
  128. {
  129.   return atoi( s );
  130. }
  131.  
  132. ushort IntInputLine::dataSize()
  133. {
  134.   return sizeof( int );
  135. }
  136.  
  137.  
  138. ////////////////////////////////////////////////////////////////////////
  139. /////////////////////// DoubleInputLine ////////////////////////////////
  140. ////////////////////////////////////////////////////////////////////////
  141.  
  142. DoubleInputLine::DoubleInputLine( const TRect& r, int maxLen,
  143.   char aMaskChar ) : NumInputLine<double>( r, maxLen, aMaskChar )
  144. {
  145. }
  146.  
  147. void DoubleInputLine::numToStr( double *rec, char *buf )
  148. {
  149.   gcvt( *rec, maxLen-3, buf );    // Save space for '-', '.', and '\0'
  150. }                                 // otherwise, no overflow or bounds check
  151.  
  152. double DoubleInputLine::strToNum( char *s )
  153. {
  154.   return atof( s );
  155. }
  156.  
  157. ushort DoubleInputLine::dataSize()
  158. {
  159.   return sizeof( double );
  160. }
  161.